import View from '@/components/View'
import React, { useState } from 'react'
import { Table, Button, Tooltip, Popconfirm, message, Input, Modal, Form, InputNumber, TreeSelect } from 'antd'
import { DeleteOutlined, FormOutlined, PlusOutlined, SearchOutlined } from '@ant-design/icons'
import { useMenuTable } from '@/hooks/auth/menu'
import { useAntdTable, useBoolean, useRequest } from 'ahooks'
import { addMenuApi, updateMenuApi, deleteMenuApi } from '@/api/auth/menu'
import './index.scss'

const AuthMenu: React.FC = () => {
  // 获取[搜索，新增/菜单]表单实例
  const [menuForm] = Form.useForm()
  const [searchForm] = Form.useForm()
  const { getMenuData, treeOptions } = useMenuTable()
  // 添加菜单 父级ID 树状选择器数据
  const [visible, { setTrue, setFalse }] = useBoolean(false)
  const [menuId, setMenuId] = useState<number>(null)
  const [modalTitle, setModalTitle] = useState<string>('添加菜单')
  // ahooks绑定表格
  const { tableProps, search } = useAntdTable(getMenuData, {
    defaultPageSize: 10,
    form: searchForm,
  })
  // 表单搜索、重置方法
  const { submit, reset } = search
  /**
   * 菜单操作接口模块
   */
  // 添加菜单接口
  const { run: addRun, loading: addLoading } = useRequest(addMenuApi, {
    manual: true,
    onSuccess: () => {
      message.success('添加菜单成功')
      setFalse()
      reset()
    },
  })
  // 修改菜单接口
  const { run: updateRun, loading: updateLoading } = useRequest(updateMenuApi, {
    manual: true,
    onSuccess: () => {
      message.success('菜单修改成功')
      setFalse()
      reset()
    },
  })
  // 删除菜单接口
  const { run: delRun } = useRequest(deleteMenuApi, {
    manual: true,
    onSuccess: () => {
      message.success('菜单删除成功')
      reset()
    },
  })
  // 判断Modal的loading
  const getLoadingVar = () => {
    switch (modalTitle) {
      case '添加菜单':
        return addLoading
      case '修改菜单':
        return updateLoading
      default:
        return null
    }
  }
  // 添加菜单弹窗
  const addMenuModal = () => {
    setModalTitle('添加菜单')
    menuForm.resetFields()
    setTrue()
  }
  // 编辑菜单弹窗
  const updateMenurModal = (params) => {
    setMenuId(params.id)
    setModalTitle('修改菜单')
    menuForm.setFieldsValue(params)
    setTrue()
  }
  // 新增、编辑菜单的提交
  const submitMenuForm = () => {
    menuForm.validateFields().then((values) => {
      switch (modalTitle) {
        case '添加菜单':
          addRun(values)
          break
        case '修改菜单':
          updateRun(menuId, values)
          break
        default:
          break
      }
    })
  }
  // 表格配置
  const columns = [
    {
      title: '菜单编码',
      dataIndex: 'code',
      key: 'code',
      ellipsis: true,
    },
    {
      title: '菜单名称',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '菜单路径',
      dataIndex: 'path',
      key: 'path',
    },
    {
      title: '权重',
      dataIndex: 'weight',
      key: 'weight',
    },
    {
      title: '创建时间',
      dataIndex: 'updateTime',
      key: 'updateTime',
    },
    {
      title: '操作',
      dataIndex: '',
      key: 'action',
      className: 'aciton-th',
      width: 120,
      fixed: 'right' as 'right',
      render: (text, record) => (
        <div className="button-box">
          <Tooltip title="编辑">
            <Button
              shape="circle"
              style={{ marginRight: '5px' }}
              type="primary"
              onClick={() => updateMenurModal(record)}
              icon={<FormOutlined />}
            />
          </Tooltip>
          <Popconfirm
            placement="topRight"
            title={<span>确认删除菜单<span style={{ color: 'red' }}>{record.name}</span></span>}
            onConfirm={() => delRun(record.id)}
            okText="确认"
            cancelText="取消"
          >
            <Tooltip title="删除">
              <Button type="primary" shape="circle" danger icon={<DeleteOutlined />} />
            </Tooltip>
          </Popconfirm>
        </div>
      ),
    },
  ]
  return (
    <View className="app-auth-menu">
      <div className="menu-top-bar">
        <div className="left-search">
          <Form
            name="menu-search-form"
            form={searchForm}
          >
            <Form.Item label="" name="code">
              <Input allowClear placeholder="请输入菜单编码" />
            </Form.Item>
            <Form.Item label="" name="name">
              <Input allowClear placeholder="请输入菜单名称" />
            </Form.Item>
            <Tooltip title="搜索">
              <Button type="primary" size="small" className="search-btn" shape="circle" onClick={submit} icon={<SearchOutlined />} />
            </Tooltip>
          </Form>
        </div>
        <Button type="primary" icon={<PlusOutlined />} onClick={addMenuModal}>添加菜单</Button>
      </div>
      <div className="menu-table">
        <Table columns={columns} rowKey={(record) => record.id} {...tableProps} pagination={false} />
      </div>
      <Modal
        title={modalTitle}
        visible={visible}
        confirmLoading={getLoadingVar()}
        onOk={submitMenuForm}
        onCancel={setFalse}
        width={400}
      >
        <Form
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 20 }}
          name="menu-item-form"
          form={menuForm}
        >
          <Form.Item label="菜单名" name="name">
            <Input />
          </Form.Item>
          <Form.Item label="菜单编号" name="code">
            <Input />
          </Form.Item>
          <Form.Item label="父级菜单" name="parentId">
            <TreeSelect treeData={treeOptions} />
          </Form.Item>
          <Form.Item label="菜单路径" name="path">
            <Input />
          </Form.Item>
          <Form.Item label="菜单图标" name="iconClass">
            <Input />
          </Form.Item>
          <Form.Item label="菜单权重" name="weight">
            <InputNumber min={1} max={999} />
          </Form.Item>
        </Form>
      </Modal>
    </View>
  )
}
export default AuthMenu
